package com.xml;



import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import org.dom4j.io.SAXReader;



/**
 *
 */
public class XmlRead
{
    public static void readXml(String... path ){
        for (int i = 0; i < path.length; i++)
        {
            File file = new File(path[i]);
            File[] files = file.listFiles();
            for (File f : files)
            {
                explan(f);
            }
        }
    }

    private static void explan(File file)
    {


        SAXReader reader = new SAXReader();
        // 如果是xml文件，则进行处理
        if (file.getName() != null && file.getName().endsWith(".xml"))
        {
            Document document = null;
            try
            {
                document = reader.read(file);
            }
            catch (DocumentException e)
            {
                System.out.println("read xml failed ,please check . file name = " + file.getName());
            }

            //根据解析的xml 来创建proto文件。
            Element root = document.getRootElement();


            Element request = root.element("Request");
            Element response = root.element("Response");




            //根据节点创建proto文件
            String name = file.getName();
            int i = name.indexOf('.');
            name = name.substring(0, i);

            if(null != request){
                List<Element> requestChilds = request.elements();
                ProtoCreate.createProtoWithEle(requestChilds, name, "Request");
            }

            if(null != response){
                List<Element> responseChilds = response.elements();
                ProtoCreate.createProtoWithEle(responseChilds, name, "Response");
            }




        }
    }
}
